package com.kingtao.message;

import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: kingtao
 * @Date: 2023-07-24-15:05
 * @Description:
 */
@Data
public abstract class Message implements Serializable {
    /**
     *  0~1     客户端请求
     *  2~3     选举leader
     *  4~5     选举secondLeader
     *  6~7     选举followerLeader（可能触发）
     *
     *  8~9     leader追加客户端日志请求
     *  10~11   secondLeader追加日志
     *  12~13   followerLeader追加日志（可能触发）
     *  14~15   follower追加日志
     *
     *  100~101 leader心跳
     *  102~103 secondLeader心跳
    */

    public static Class<? extends Message> getMessageClass(int type) {
        return messageClasses.get(type);
    }

    private static final Map<Integer, Class<? extends Message>> messageClasses = new HashMap<Integer, Class<? extends Message>>();

    public abstract int getMessageType();

    static {

        messageClasses.put(111, ServerInitRequest.class);
        messageClasses.put(112, ServerInitResponse.class);

        messageClasses.put(0, ClientRequest.class);
        messageClasses.put(1, ClientResponse.class);
        messageClasses.put(2, SelectLeaderRequest.class);
        messageClasses.put(3, SelectLeaderResponse.class);
        messageClasses.put(4, SelectSecondLeaderRequest.class);
        messageClasses.put(5, SelectSecondLeaderResponse.class);
        messageClasses.put(6, SelectFollowerLeaderRequest.class);
        messageClasses.put(7, SelectFollowerLeaderResponse.class);

        messageClasses.put(8, AppendLeaderLogEntryRequest.class);
        messageClasses.put(9, AppendLeaderLogEntryResponse.class);
        messageClasses.put(10, AppendSecondLeaderLogEntryRequest.class);
        messageClasses.put(11, AppendSecondLeaderLogEntryResponse.class);
        messageClasses.put(12, AppendFollowLeaderLogEntryRequest.class);
        messageClasses.put(13, AppendFollowLeaderLogEntryResponse.class);
        messageClasses.put(14, AppendFollowerLogEntryRequest.class);
        messageClasses.put(15, AppendFollowerLogEntryResponse.class);

        messageClasses.put(16, AppendNodeRequest.class);
        messageClasses.put(17, AppendNodeResponse.class);

        messageClasses.put(18, GroupSessionRequest.class);
        messageClasses.put(19, GroupSessionResponse.class);
        messageClasses.put(20, GroupServiceRequest.class);
        messageClasses.put(21, GroupServiceResponse.class);
    }
}
